package com.foreverwin.jzymes.production.tool.service.impl;

import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.tool.domain.ToolBill;
import com.foreverwin.jzymes.production.tool.domain.ToolBillItem;
import com.foreverwin.jzymes.production.tool.domain.ToolUsageLog;
import com.foreverwin.jzymes.production.tool.mapper.ToolUsageLogAdviceMapper;
import com.foreverwin.jzymes.production.tool.mapper.ToolUsageLogMapper;
import com.foreverwin.jzymes.production.tool.service.IToolBillAdviceService;
import com.foreverwin.jzymes.production.tool.service.IToolBillItemService;
import com.foreverwin.jzymes.production.tool.service.IToolUsageAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
public class ToolUsageAdviceServiceImpl implements IToolUsageAdviceService {

    @Autowired
    IToolBillAdviceService toolBillAdviceService;
    @Autowired
    IToolBillItemService toolBillItemService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ToolUsageLogAdviceMapper toolUsageLogAdviceMapper;
    @Autowired
    ToolUsageLogMapper toolUsageLogMapper;

    /**
     * 累计设备刀具使用寿命
     *
     * @param site
     * @param machineType
     * @param resource
     */
    public void addToolUsage( String site, String machineType, String resource ){


        String cncType = resourceAdviceService.findCncType( site, resource );
        Assert.hasText( cncType, I18nUtil.getI18nText( "common.resourceCncTypeNotExist", new String[]{ resource } ) );

        /**查询当前版本刀具清单*/
        ToolBill toolBill = toolBillAdviceService.selectToolBillByMachineTypeCncType( site, machineType, cncType );
        if( toolBill == null ){
            return;
        }

        /**查询刀具清单刀号信息*/
        ToolBillItem querySet = new ToolBillItem();
        querySet.setToolBillBo( toolBill.getHandle() );
        List<ToolBillItem> toolBillItems = toolBillItemService.selectToolBillItemList( querySet );
        if( toolBillItems == null || toolBillItems.size() == 0 ){
            return;
        }

        /**累加刀具使用寿命*/
        Date startDateTime = new Date();
        for( ToolBillItem toolBillItem : toolBillItems ){
            ToolUsageLog toolUsageLog = toolUsageLogAdviceMapper.selectToolUsageLogByResourceLoc( site, machineType, resource, toolBillItem.getToolLoc() );
            if( toolUsageLog != null ){
                toolUsageLog.setUsageCount( toolUsageLog.getUsageCount() + 1 );
                toolUsageLogMapper.updateToolUsageLog( toolUsageLog );
            }else{
                toolUsageLog = new ToolUsageLog();
                toolUsageLog.setSite( site );
                toolUsageLog.setMachineType( machineType );
                toolUsageLog.setResrce( resource );
                toolUsageLog.setToolLoc( toolBillItem.getToolLoc() );
                toolUsageLog.setUsageCount( 1L );
                toolUsageLog.setStartDateTime( startDateTime );
                toolUsageLog.setHandle( BOHandleUtil.buildToolUsageLogHandle( toolUsageLog.getSite(), toolUsageLog.getResrce(), toolUsageLog.getToolLoc(), toolUsageLog.getStartDateTime() ) );
                toolUsageLogMapper.insertToolUsageLog( toolUsageLog );
            }

        }
    }

}
